import type { PaginationConfig } from '@/types';
import type { RequestResult } from '@/utils/request';
import { ServiceProxy } from '@/constant';
import { request } from '@umijs/max';
import type { ArticleRecord } from '@/DatabaseType/Article';

// 获取角色列表
export async function getArticleList(
  params: Pick<ArticleRecord, 'id' | 'headline' | 'outline' | 'tagIds'> &
    PaginationConfig & {
      author: string;
    },
): Promise<
  RequestResult<{
    total: number;
    list: ArticleRecord[];
  }>
> {
  return request(`${ServiceProxy.DefaultPrefix}/article`, {
    method: 'GET',
    params,
  });
}

// 获取详情
export async function getArticle(
  id: number,
): Promise<RequestResult<ArticleRecord>> {
  return request(`${ServiceProxy.DefaultPrefix}/article/${id}`, {
    method: 'GET',
  });
}

// 创建
export async function addArticle(
  data: Pick<
    ArticleRecord,
    'coverUrl' | 'headline' | 'outline' | 'status' | 'tagIds' | 'userId'
  >,
): Promise<RequestResult<number>> {
  return request(`${ServiceProxy.DefaultPrefix}/article`, {
    method: 'POST',
    data,
  });
}

// 更新
export async function updateArticle(
  id: number,
  data: Pick<
    ArticleRecord,
    'coverUrl' | 'headline' | 'outline' | 'status' | 'tagIds' | 'userId'
  >,
): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/article/${id}`, {
    method: 'PUT',
    data,
  });
}

// 删除
export async function deleteArticle(id: number): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/article/${id}`, {
    method: 'DELETE',
  });
}
